<?php
class EbookController extends Core\Api {
    public function downAction() {
        $user_id = $this->session['user']['id'];
        $user_id =1;
        if(!$user_id) {
            header('HTTP/1.1 403 Forbidden');
            echo '没有登录';
            return ;
        }
        $book_id = $this->input('book_id');
        if(!$book_id) {
            header('HTTP/1.1 404 Not Found');
            echo '没有book_id';
            return ;
        }
        $book = \Business\UserModel::getBook($user_id,$book_id);
        if (!$book) {
            header('HTTP/1.1 403 Forbidden');
            echo '没有此书或暂时没有权限阅读此书可能因为没有购买';
            return ;
        }
        $book_id = $book['book_id'];
        $book_ver = $book['version'];
        $version = $this->input('version');
        if ($version == $book_ver && !$version) {
            header('HTTP/1.1 304 Not Modified');
        }
        $time = time();
        $unpack_path = BASE_PATH.'public/'.$book['unpack_path'];
        $file = new Epub\Resource\File($unpack_path);
        $epub = new Epub\Epub($file);
        $container = $epub->getContainer();
        $rootfile = $container->rootfile;
        $container->setRootbooks($rootfile);
        $container->rootfile = 'SSWC/content.opf';
        $container->setBookinfo($user_id,$book_id,$book_ver,$time);
        $datalist = $this->fetchDir($unpack_path);
        $zip = new ZipArchive;
        $filename = '/tmp/'.md5($book_id.$user_id).'.zip';
        if ($zip->open($filename, ZipArchive::OVERWRITE |ZipArchive::CREATE) === TRUE) {
            foreach ($datalist as $val) {
                if(file_exists($val)){
                    $zip->addFile($val,str_replace($unpack_path.DS,'',$val));//第二个参数是放在压缩包中的文件名称，如果文件可能会有重复，就需要注意一下
                }
            }
            $zip->addFromString($container->getFilename(),(string)$container);
            $bool = $zip->close();
        }
        $dw = new Download($book_id.'.zip',$filename); //下载文件
        $dw->getfiles();
    }
    public function fetchDir($dir) {
        $data = array();
        foreach(glob($dir.DS.'*') as $file) {
            if(is_dir($file)) {
                $sub_data = self::fetchDir($file);
                $data = array_merge($data,$sub_data);
            }else{
                $data[] = $file;
            }
        }
        return $data;
    }
    //书架
    public function shelvesAction(){
        $user_id = $this->session->user['id'];
        if (!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $data = Business\UserModel::getBookList($user_id);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    public function delFromShelvesAction(){
        $book_id = $this->post('book_id');
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $data = Business\UserModel::delFormShelves($user_id,$book_id);
        $ret = $this->ok($data);
        return $this->json($ret);
    }
    public function contentsAction() {
        $user_id = $this->session['user']['id'];
        $user_id =1;
        $book_id = $this->post('book_id');
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        if(!$book_id) {
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $book = \Business\UserModel::getBook($user_id,$book_id);
        if (!$book) {
            $ret = $this->error('没有此书或暂时没有权限阅读此书可能因为没有购买');
            return $this->json($ret);
        }
        $unpack_path = BASE_PATH.'public/'.$book['unpack_path'];
        $file = new Epub\Resource\File($unpack_path);
        $epub = new Epub\Epub($file);
        $data = $epub->getContainer()->getOpf()->getContents();
        $ret = $this->ok($data);
        $this->json($ret);
    }
    public function pagesAction() {
        $user_id = $this->session['user']['id'];
        $user_id =1;
        $book_id = $this->post('book_id');
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        if(!$book_id) {
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $book = \Business\UserModel::getBook($user_id,$book_id);
        if (!$book) {
            $ret = $this->error('没有此书或暂时没有权限阅读此书可能因为没有购买');
            return $this->json($ret);
        }
        $unpack_path = BASE_PATH.'public/'.$book['unpack_path'];
        $file = new Epub\Resource\File($unpack_path);
        $epub = new Epub\Epub($file);
        $data = $epub->getContainer()->getOpf()->getMedia('pages');
        $ret = $this->ok($data);
        $this->json($ret);
    }
    public function bodyAction() {
        $user_id = $this->session['user']['id'];
        $book_id = $this->post('book_id');
        $src = $this->post('src');
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        if(!$book_id) {
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        if(!$src) {
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $book = \Business\UserModel::getBook($user_id,$book_id);
        if (!$book) {
            $ret = $this->error('没有此书或暂时没有权限阅读此书可能因为没有购买');
            return $this->json($ret);
        }
        $unpack_path = BASE_PATH.'public/'.$book['unpack_path'];
        $file = new Epub\Resource\File($unpack_path);
        $epub = new Epub\Epub($file);
        $data = $epub->getContainer()->getOpf()->getBody($src);
        print_R($data);
    }
}
